package com.example.week8_mybatis_plus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.week8_mybatis_plus.entity.Goods;
import com.example.week8_mybatis_plus.entity.Supplier;
import com.example.week8_mybatis_plus.mapper.SupplierMapper;
import com.example.week8_mybatis_plus.service.ISupplierService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangbo
 * @since 2022-10-25
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements ISupplierService {
    @Autowired
    private SupplierMapper supplierMapper;
    @Override
    public Page<Supplier> getSupplier(Map<String, Object> cond, Page<Supplier> page) {
        LambdaQueryWrapper<Supplier> lqw = new LambdaQueryWrapper<>();
        lqw.eq(cond.containsKey("name"),Supplier::getName, cond.get("name"))
                .like(cond.containsKey("address"), Supplier::getAddress, cond.get("address"))
                .eq(cond.containsKey("phone"), Supplier::getPhone, cond.get("phone"))
                .eq(cond.containsKey("id"), Supplier::getId, cond.get("id"));
        return supplierMapper.selectPage(page,lqw);
    }

    @Override
    public Supplier addSupplier(Supplier supplier) {
        supplier.setId(null);
        if (supplierMapper.insert(supplier) > 0) {
            return supplier;
        }
        return null;
    }

    @Override
    public Supplier updateSupplier(Supplier supplier) {
        if (supplierMapper.updateById(supplier) > 0) {
            return supplier;
        }
        return null;
    }

    @Override
    public boolean deleteSupplier(int id) {
        return supplierMapper.deleteById(id) > 0;
    }
}
